#-*- coding: utf-8 -*-
import pandas as pd
inputfile = '../tmp/data3_GM11.xls'  # 灰色预测后保存的路径
outputfile = '../data/sales_tax.xls'  # 神经网络预测后保存的结果
modelfile = '../tmp/3-net.model'  # 模型保存路径
data = pd.read_excel(inputfile)  # 读取数据
feature = ['x3', 'x4', 'x6', 'x8']  # 特征所在列

data_train = data.loc[range(1999, 2014)].copy()  # 取2014年前的数据建模
data_mean = data_train.mean()
data_std = data_train.std()
data_train = (data_train - data_mean) / data_std  # 数据标准化
x_train = data_train[feature].as_matrix()  # 特征数据
y_train = data_train['y'].as_matrix()  # 标签数据

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation

model = Sequential()  # 建立模型
model.add(Dense(4, 8))
model.add(Activation('relu'))  # 用relu函数作为激活函数，能够大幅提供准确度
model.add(Dense(8, 1))
model.compile(loss='mean_squared_error', optimizer='adam')  # 编译模型
model.fit(x_train, y_train, nb_epoch=10000, batch_size=16)  # 训练模型，学习一万次
model.save_weights(modelfile)  # 保存模型参数

# 预测，并还原结果。
x = ((data[feature] - data_mean[feature]) / data_std[feature]).as_matrix()
data[u'y_pred'] = model.predict(x) * data_std['y'] + data_mean['y']
data[u'y_pred'] = data[u'y_pred'].round(2)
data.to_excel(outputfile)

import matplotlib.pyplot as plt  # 画出预测结果图
p = data[['y', 'y_pred']].plot(subplots=True, style=['b-o', 'r-*'])
plt.show()
